using System.Collections.Generic;
using UnityEngine;

public enum PanelType
{
    Panel,Tip,Black
}

public static class PanelManager
{
    [Header("面板和位置字典")]
    public static Dictionary<string, BasePanel> panels = new Dictionary<string, BasePanel>();
    public static Dictionary<PanelType, Transform> panelPos = new Dictionary<PanelType, Transform>();
    [Header("根节点")]
    public static Transform root;

    //初始化
    public static void Init()
    {
        //清空字典
        panels.Clear();
        panelPos.Clear();
        //获取场景里面的Transform
        root = GameObject.Find("Root").transform;
        Transform panel = root.Find("Panel");
        Transform tip = root.Find("Tip");
        Transform black = root.Find("Black");
        //添加字典
        panelPos.Add(PanelType.Panel, panel);
        panelPos.Add(PanelType.Tip, tip);
        panelPos.Add(PanelType.Black, black);
    }
    //打开
    public static void Open<T>(params object[] par) where T : BasePanel
    {
        //判断是否打开
        string myName = typeof(T).Name;
        if (myName!="DiePanel")
        {
            //在战斗中禁止打开其他UI,放置打断
            if (GameManager.Instance.isBattle) return;
        }
        if (panels.ContainsKey(myName)) return;
        //添加脚本
        BasePanel panel = root.gameObject.AddComponent<T>();
        //添加字典
        panels.Add(myName, panel);
        //初始化
        panel.OnInit();
        panel.Init();
        //实例化，选择父物体
        panel.item.transform.SetParent(panelPos[panel.type],false);
        //展示
        panel.OnShow(par);
    }
    //关闭
    public static void Close(string myName)
    {
        //判断是否关闭
        if (!panels.ContainsKey(myName)) return;
        //获取脚本
        BasePanel panel = panels[myName];
        //执行关闭
        panel.OnClose();
        //移除字典
        panels.Remove(myName);
        //销毁面板
        MonoBehaviour.Destroy(panel.item);
        //销毁脚本
        Component.Destroy(panel);
    }
}